package com.gcb.common.enums;

import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 职务资格类别
 */
@Getter
public enum CredentialsTypeEnum {
    CAPTAIN(1, "船长"),
    FIRST_MATE(2, "大副"),
    SECOND_MATE(3, "二副"),
    THIRD_MATE(4, "三副"),
    SENIOR_WATCHMAN(5, "高级值班水手"),
    WATCHMAN(6, "值班水手"),
    CHIEF_ENGINEER(7, "轮机长"),
    FIRST_ENGINEER(8, "大管轮"),
    SECOND_ENGINEER(9, "二管轮"),
    THIRD_ENGINEER(10, "三管轮"),
    ELECTRO_TECHNICAL_OFFICER(11, "电子电气员"),
    SENIOR_ENGINE_WATCH(12, "高级值班机工"),
    ENGINE_WATCH(13, "值班机工"),
    ELECTRONIC_TECHNICIAN(14, "电子技工"),
    FIRST_RADIO_ELECTRONICS_OFFICER(15, "一级无线电电子员"),
    SECOND_RADIO_ELECTRONICS_OFFICER(16, "二级无线电电子员"),
    GENERAL_OPERATOR(17, "通用操作员"),
    RESTRICTED_OPERATOR(18, "限用操作员");

    private final Integer code;
    private final String info;

    CredentialsTypeEnum(Integer code, String info) {
        this.code = code;
        this.info = info;
    }

    public static List<Integer> getCodeListByInfo(String info) {
        List<Integer> codeList = new ArrayList<>();
        for (CredentialsTypeEnum credentialsType : CredentialsTypeEnum.values()){
            if (credentialsType.getInfo().indexOf(info) != -1){
                codeList.add(credentialsType.getCode());
            }
        }
        return codeList;
    }

    public static String getInfo(String type) {
        return Optional.ofNullable(of(Integer.valueOf(type)))
                .map(CredentialsTypeEnum::getInfo)
                .orElse("");
    }

    public static String getInfo(Integer type) {
        return Optional.ofNullable(of(type))
                .map(CredentialsTypeEnum::getInfo)
                .orElse("");
    }

    public static Integer getCode(String info) {
        return Optional.ofNullable(ofCode(info))
                .map(CredentialsTypeEnum::getCode)
                .orElse(null);
    }

    public static CredentialsTypeEnum of(Integer code) {
        Optional<CredentialsTypeEnum> first = Arrays.stream(CredentialsTypeEnum.values()).filter(v -> v.getCode().equals(code)).findFirst();
        return first.orElse(null);
    }

    public static CredentialsTypeEnum ofCode(String info) {
        Optional<CredentialsTypeEnum> first = Arrays.stream(CredentialsTypeEnum.values()).filter(v -> v.getInfo().indexOf(info) != -1).findFirst();
        return first.orElse(null);
    }
}
